package ru.tennisworld.user.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import ru.tennisworld.user.dto.UpdateTennisUserRequestDto;
import ru.tennisworld.user.enums.ValidationErrorEnum;
import ru.tennisworld.user.exceptions.TennisUserValidationException;
import ru.tennisworld.user.service.ValidationControllerService;
import ru.tennisworld.user.utils.ValidationResult;

@Service("requestValidationService")
public class ValidationControllerServiceImpl implements ValidationControllerService {

    @Override
    public void validateUpdateTennisUserRequest(UpdateTennisUserRequestDto request) {
        ValidationResult validationResult = new ValidationResult();
        validationResult.mergeValidation(validateUpdateTennisUserMainInfo(request));
        if (validationResult.isValidationError()) {
            throw new TennisUserValidationException("Ошибка валидации запроса", validationResult);
        }
    }

    private ValidationResult validateUpdateTennisUserMainInfo(UpdateTennisUserRequestDto request) {
        ValidationResult validationResult = new ValidationResult();
        if (StringUtils.isEmpty(request.getUserPassword())) {
            validationResult.addError(ValidationErrorEnum.TENNIS_USER_PASSWORD_NULL);
        } else if (StringUtils.isEmpty(request.getConfirmationPassword()) ||
                (!StringUtils.isEmpty(request.getConfirmationPassword()) &&
                        !request.getConfirmationPassword().equals(request.getUserPassword()))) {
            validationResult.addError(ValidationErrorEnum.TENNIS_USER_REQUEST_CONFIRMATION_PASSWORD);
        }
        return validationResult;
    }
}
